package com.hy.example.shared.utils;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.spring.SpringMVCUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.hy.example.shared.autoconfigure.common.SharedProperties;
import com.hy.mining.sa.model.UserDTO;
import com.hy.example.shared.autoconfigure.common.SharedProperties;
import com.hy.example.shared.autoconfigure.common.SharedProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.Optional;

@Slf4j
public class UserAuditorHelper {

    /**
     * 当前线程的 token 信息
     */
    private static final ThreadLocal<String> CURRENT_TOKEN = new ThreadLocal<>();

    @Autowired
    private Environment environment;
    @Autowired
    private SharedProperties sharedProperties;

    public static Optional<UserDTO> getCurrentUser() {
        String token = CURRENT_TOKEN.get();
        if (StringUtils.hasLength(token)) {
            Object loginId = StpUtil.getLoginIdByToken(token);
            if (Objects.nonNull(loginId)) {
                UserDTO userDTO = (UserDTO) StpUtil.getSessionByLoginId(loginId).get(SaSession.USER);
                return Optional.ofNullable(userDTO);
            }
        }
        if (SpringMVCUtil.isWeb() && StpUtil.isLogin()) {
            UserDTO userDTO = (UserDTO) StpUtil.getSession().get(SaSession.USER);
            return Optional.ofNullable(userDTO);
        }
        return Optional.empty();
    }

    public static Optional<String> retrieveCurrentToken() {
        String token = CURRENT_TOKEN.get();
        if (StringUtils.hasLength(token)) {
            return Optional.of(token);
        }
        if (SpringMVCUtil.isWeb()) {
            token = StpUtil.getTokenValue();
            if (StringUtils.hasLength(token)) {
                return Optional.of(token);
            }
        }
        return Optional.empty();
    }

    public boolean flushToken() {
        if (StpUtil.isLogin()) {
            StpUtil.renewTimeout(sharedProperties.getToken().getExpireTime());
            return true;
        }
        return false;
    }

    public boolean flushToken(String token) {
        long tokenTimeout = StpUtil.getTokenTimeout(token);
        if (tokenTimeout == -2) {
            return false;
        }
        StpUtil.renewTimeout(token,sharedProperties.getToken().getExpireTime());
        return true;
    }

    public boolean validToken() {
        return StpUtil.isLogin();
    }

    public boolean validToken(String token) {
        long tokenTimeout = StpUtil.getTokenTimeout(token);
        return tokenTimeout != -2;
    }

    /**
     * 设置 token
     *
     * @param currentToken token
     */
    public static void setCurrentToken(String currentToken) {
        CURRENT_TOKEN.set(currentToken);
    }

    public static void removeCurrentToken() {
        CURRENT_TOKEN.remove();
    }


}
